package com.yjt.fresh.adpater.adpsection;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.yjt.fresh.R;
import com.yjt.fresh.bean.BeanCart;
import com.yjt.fresh.bean.BeanCartChange;
import com.yjt.fresh.bean.BeanStoreCoupon;
import com.yjt.fresh.constant.MyConstants;

import java.math.BigDecimal;
import java.util.List;

/**
 * 结算页面的适配器
 * Created by mcc on 2018-08-06.
 */

public class AdpRvAtyBalance extends SectionedRecyclerViewAdapter<AdpRvAtyBalanceHeadHolder, AdpRvAtyBalanceContentHolder, AdpRvAtyBalanceFootHolder> {
    private final List<BeanCartChange> mCartChangeList;
    private Context mContext;

    public AdpRvAtyBalance(Context context, List<BeanCartChange> cartChangeList) {
        mContext = context;
        mCartChangeList = cartChangeList;
    }

    @Override
    protected int getSectionCount() {
        return mCartChangeList == null ? 0 : mCartChangeList.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        return mCartChangeList.get(section).getBeanCarts().size();
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return true;
    }

    @Override
    protected AdpRvAtyBalanceHeadHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new AdpRvAtyBalanceHeadHolder(View.inflate(mContext, R.layout.app_recycle_item_aty_balance_head, null));
    }

    @Override
    protected AdpRvAtyBalanceFootHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return new AdpRvAtyBalanceFootHolder(View.inflate(mContext, R.layout.app_recycle_item_aty_balance_foot, null));
    }

    @Override
    protected AdpRvAtyBalanceContentHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new AdpRvAtyBalanceContentHolder(View.inflate(mContext, R.layout.app_recycle_item_aty_balance_content, null));
    }

    @Override
    protected void onBindSectionHeaderViewHolder(AdpRvAtyBalanceHeadHolder holder, int section) {
        holder.getTvStoreName().setText(mCartChangeList.get(section).getStoreName());
    }

    @Override
    protected void onBindSectionFooterViewHolder(final AdpRvAtyBalanceFootHolder holder, final int section) {

        holder.getRlInvoice().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSectionItemClickListener != null){
                    mOnSectionItemClickListener.onSectionItemClick(MyConstants.BALANCE_ITEM_FOOT,section,-1,v.getId(),holder.getTvInvoice(),null);
                }
            }
        });

        holder.getRlCoupon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSectionItemClickListener != null){
                    mOnSectionItemClickListener.onSectionItemClick(MyConstants.BALANCE_ITEM_FOOT,section,-1,v.getId(),holder.getTvCoupon(),holder.getTvCouponMoney());
                }
            }
        });
        List<BeanStoreCoupon> couponForPays = mCartChangeList.get(section).getCouponForPays();
        if (couponForPays != null && couponForPays.size() > 0){
            holder.getTvCouponCount().setVisibility(View.VISIBLE);
            holder.getTvCouponCount().setText(couponForPays.size()+"张可用");
            holder.getTvCoupon().setText("请选择");
        }else {
            holder.getTvCoupon().setText("无可用");
            holder.getRlCoupon().setClickable(false);
            holder.getTvCouponCount().setVisibility(View.GONE);
        }
        double totalPrice = 0;
        final BeanCartChange beanCartChange = mCartChangeList.get(section);
        List<BeanCart> goodsList = beanCartChange.getBeanCarts();
        for (int i = 0; i < goodsList.size(); i++) {
            totalPrice += goodsList.get(i).getGOODINFO().getSINGLE_PRICE()*goodsList.get(i).getCOUNT();
        }
        BigDecimal bigDecimal = new BigDecimal(totalPrice);
        totalPrice = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        holder.getTvMoney().setText("¥  "+totalPrice);
        if (mCartChangeList.get(section).getCouponMoney() > 0) {
            holder.getTvCoupon().setText("- ¥ "+mCartChangeList.get(section).getCouponMoney());
            holder.getTvCouponMoney().setText("- ¥ "+mCartChangeList.get(section).getCouponMoney());
            holder.getTvCouponCount().setVisibility(View.VISIBLE);
            holder.getTvCouponCount().setBackgroundResource(R.drawable.zz_shape_43b624_5);
            holder.getTvCouponCount().setText("已选1张");
        }else {
            holder.getTvCouponMoney().setText("- ¥  0.00 ");
        }

        holder.getEtMessage().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                beanCartChange.setRemark(s.toString());
            }
        });
    }

    @Override
    protected void onBindItemViewHolder(AdpRvAtyBalanceContentHolder holder, int section, int position) {
        BeanCart beanCart = mCartChangeList.get(section).getBeanCarts().get(position);
        String sku_img = beanCart.getGOODINFO().getSKU_IMG();
        Glide.with(mContext).load(sku_img).into(holder.getIv());
        holder.getTvTitle().setText(beanCart.getGOODINFO().getTITLE());
        holder.getTvNewPrice().setText(""+beanCart.getGOODINFO().getSINGLE_PRICE());
        holder.getTvCount().setText("x"+beanCart.getCOUNT());
    }

    private OnSectionItemClickListener mOnSectionItemClickListener;

    public void setOnSectionItemClickListener(OnSectionItemClickListener onSectionItemClickListener) {
        mOnSectionItemClickListener = onSectionItemClickListener;
    }
}
